<template>
  <div>
    <div class="swiper-container">
      <div class="swiper-wrapper">
        <img
          class="swiper-slide"
          v-for="item in bannerList"
          :key="item.ID"
          :src="item.Cpic"
          :alt="item.Cname"
        />
      </div>
      <div class="swiper-pagination">
        <span
          class="swiper-pagination-bullet swiper-pagination-bullet-active"
        ></span>
        <span class="swiper-pagination-bullet"></span>
        <span class="swiper-pagination-bullet"></span>
        <span class="swiper-pagination-bullet"></span>
        <span class="swiper-pagination-bullet"></span>
      </div>
    </div>
    <div class="header">
      <div class="weather" @click="goAreaWeather">
        <img src="@/assets/images/theIndex/duoyun.png" alt="多云" />
        <span>13℃</span>
      </div>
      <div class="input">
        <input
          type="search"
          form=""
          placeholder="Webpack实战教程（从入门到精通）"
        />
        <i class="iconfont icon-sousuo"></i>
      </div>
      <div class="add iconfont icon-jia"></div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import Swiper from "swiper";
import "swiper/swiper-bundle.css";
export default {
  name: "TheIndex",
  data() {
    return {
      bannerList: [],
    };
  },
  methods: {
    // 跳转天气页
    goAreaWeather() {
      this.$router.push("/areaWeather");
    },
    // 跳转讲师详情
    goTeacherDetail() {
      this.$router.push("/teacherDetail");
    },
    // 跳转学习页
    goToStudy() {
      this.$router.push("/toStudy");
    },
  },
  mounted() {
    axios
      .get("http://146.56.207.108:3000/Handler/CourseHandler?action=courseshow")
      .then((response) => {
        console.log(response.data.data.list);
        // 获取请求结果中最热的三张图片
        this.bannerList = response.data.data.list.slice(0, 5);

        // 初始化Swiper实例
        new Swiper(".swiper-container", {
          pagination: {
            el: ".swiper-pagination",
          },
          on: {
            slideChange: function () {
              // 获取所有分页器元素
              let bullets = document.querySelectorAll(
                ".swiper-pagination-bullet"
              );

              // 移除之前的激活状态样式
              for (let i = 0; i < bullets.length; i++) {
                bullets[i].classList.remove("swiper-pagination-bullet-active");
              }

              console.log("图片切换了", this.activeIndex);
              // 为当前激活状态的分页器元素添加样式
              bullets[this.activeIndex].classList.add(
                "swiper-pagination-bullet-active"
              );
            },
          },
        });
      })
      .catch((error) => {
        console.error(error);
      });
  },
};
</script>

<style lang="less" scoped>
.swiper-container {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 3.4561rem;
  overflow: hidden;

  .swiper-slide {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .swiper-pagination {
    display: flex;
    align-items: center;
    justify-content: space-around;
    position: absolute;
    z-index: 2;
    bottom: 0.0999rem;
    left: 50%;
    transform: translateX(-50%);
    .swiper-pagination-bullet {
      float: left;
      width: 0.1598rem;
      height: 0.1598rem;
      border-radius: 50%;
      margin: 0 0.0599rem;
      background-color: #eee;
    }
    .swiper-pagination-bullet-active {
      background-color: #f39c12;
    }
  }
}

.header {
  display: flex;
  align-items: center;
  position: absolute;
  z-index: 2;
  top: 0.3995rem;
  width: 100%;
  height: 0.899rem;
  box-sizing: border-box;
  padding: 0 0.2897rem;

  .weather {
    height: 100%;
    color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 0.2397rem;

    img {
      width: 0.4195rem;
    }
  }
  .input {
    width: 5.4937rem;
    height: 100%;
    margin-left: 0.1998rem;
    position: relative;

    input {
      width: 5.4937rem;
      height: 0.4994rem;
      border: none;
      outline: none;
      border-radius: 0.4495rem;
      font-size: 0.2397rem;
      text-indent: 0.6992rem;
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
    }

    i {
      font-size: 0.3196rem;
      color: #5d5d5d;
      position: absolute;
      left: 0.1998rem;
      top: 50%;
      transform: translateY(-50%);
    }
  }

  .add {
    font-size: 0.3196rem;
    color: #fff;
    position: absolute;
    top: 50%;
    right: 0.2997rem;
    transform: translateY(-50%);
  }
}
</style>
